import os
import sys
import time
import json
import django
import uuid
import datetime

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.extend([BASE_DIR, ])
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "finance_c2c.settings")
django.setup()
from core.wchat.mini_base import wechat_client, wechat_client_howhow
from apps.coffee.models import CouponInfo

from django.contrib.auth import get_user_model

UserModel = get_user_model()

# 到账通知
def coupon_message_notice():
    now_time = datetime.datetime.now()
    message_time = now_time.strftime("%Y-%m-%d %H:%M:%S")
    end_time_7 = now_time + datetime.timedelta(days=7)
    end_time_7_str = end_time_7.strftime("%Y-%m-%d %H:%M:%S")
    week_index = now_time.weekday()  # 1
    # weekday 对应的是 周几，周日是1，周一是2，到周六是 7
    day_zip = {0: 2, 1: 3, 2: 4, 3: 5, 4: 6, 5: 7, 6: 1}
    weekday = day_zip[week_index]
    # weekday = []
    # weekday = 1
    # (1, 7) (2, 1) (3, 2) (4, 3) (5, 4) (6, 5) (7, 6) django
    # (0, 1) (1, 2) (2, 3) (3, 4) (4, 5) (5, 6) (6, 7)
    queryset = UserModel.objects.filter(wx_mini_user__isnull=False, create_time__week_day=weekday)
    for i in queryset:
        coupon_info = CouponInfo.objects.one_create_by_normal(i, expire_days=7, quota=1300, name="13元立减券")
        name = coupon_info.name
        data = {
            "thing1": {
                "value": name
            },
            "thing6": {
                "value": "1张"
            },
            "time5": {
                "value": message_time
            },
            "time7": {
                "value": end_time_7_str
            },
            "thing8": {
                "value": '优惠券可在"我的-优惠券管理"中进行查看'
            },
        }
        if i.wx_mini_user:
            openid = i.wx_mini_user.openid
            if openid == "opiob5CInQoUmR0zg1Xl7GfQ-Jkw":
                print(True)
            # print(i.wx_mini_user.nick_name)
            template_id = "VK9-GE2izyqlB6Zv9v7ggHsRzv_t0_70aiGZf4MeWTc"
            try:
                # openid = "oKtsR5Y63MhZ8Jp1oYiABJ3i8dJs"
                response = wechat_client.wechat_extra.mini_send_subscribe_template(openid, template_id, data,
                                                                                   "pages/coupon/coupon",
                                                                                   miniprogram_state="formal")
                # print("response", response, i.mobile)
                # print(openid)
                print("response", response, i.mobile, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
            except Exception as e:
                # print(e, i.id)
                print(e, i.id, i.mobile, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))



def coupon_message_notice_howho():
    now_time = datetime.datetime.now()
    message_time = now_time.strftime("%Y-%m-%d %H:%M:%S")
    end_time_7 = now_time + datetime.timedelta(days=7)
    end_time_7_str = end_time_7.strftime("%Y-%m-%d %H:%M:%S")
    week_index = now_time.weekday()  # 1
    # weekday 对应的是 周几，周日是1，周一是2，到周六是 7
    day_zip = {0: 2, 1: 3, 2: 4, 3: 5, 4: 6, 5: 7, 6: 1}
    weekday = day_zip[week_index]
    # weekday = []
    # weekday = 1
    # (1, 7) (2, 1) (3, 2) (4, 3) (5, 4) (6, 5) (7, 6) django
    # (0, 1) (1, 2) (2, 3) (3, 4) (4, 5) (5, 6) (6, 7)
    queryset = UserModel.objects.filter(wx_mini_user__isnull=False, create_time__week_day=weekday)
    for i in queryset:
        coupon_info = CouponInfo.objects.one_create_by_normal(i, expire_days=7, quota=1300, name="13元立减券")
        name = coupon_info.name
        data = {
            "thing1": {
                "value": name
            },
            "thing6": {
                "value": "1张"
            },
            "time5": {
                "value": message_time
            },
            "time7": {
                "value": end_time_7_str
            },
            "thing8": {
                "value": '优惠券可在"我的-优惠券管理"中进行查看'
            },
        }
        if i.wx_mini_user:
            openid = i.wx_mini_user.openid
            if openid == "opiob5CInQoUmR0zg1Xl7GfQ-Jkw":
                print(True)
            # print(i.wx_mini_user.nick_name)
            template_id = "8GVsuDsPRfjGfm_FEVlXuafIIWEis_YE_kK3A114kkM"
            try:
                # openid = "oKtsR5Y63MhZ8Jp1oYiABJ3i8dJs"
                response = wechat_client_howhow.wechat_extra.mini_send_subscribe_template(openid, template_id, data,
                                                                                   "pages/coupon/coupon",
                                                                                   miniprogram_state="formal")
                print("response", response)
                print(openid)
            except Exception as e:
                # print(e, i.id)
                pass



if __name__ == '__main__':
    coupon_message_notice()
